package com.fh.controller.system.tools;

import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.util.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import javax.xml.crypto.Data;
import java.util.List;

/**
 * Created by caozengling on 2017/9/7.
 */
public class GetDatalayui {
    protected static Logger logger = Logger.getLogger(GetDatalayui.class);
    public static void getPData(Page page, PageData pd) {
        //分页暂时设置状态
        page.setShowCount(Integer.parseInt(pd.get("limit").toString()));
        page.setCurrentPage(Integer.parseInt(pd.get("page").toString()));
        page.setPd(pd);
    }

    public static String getLData(Page page, List<PageData> varList) {
        String start="{\"code\":0,\"msg\":\"\",\"count\":" +page.getTotalResult()+",\"data\":";
        String end="}";
        //返回layui table接收的json数据
        String datalayui = null;
        if (varList != null && varList.size() > 0) {
            for (int i = 0; i < varList.size(); i++) {
                varList.get(i).put("ROWNUM", (page.getCurrentPage() - 1) * 10 + i + 1);
            }
        }
        datalayui= JSONHelper.array2json(varList);
        datalayui=start+datalayui+end;
        return datalayui;
    }
    public static String getusername(){
        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);
        return user.getUSERNAME();
    }

    public static int getPageTotal(int num,int pageCount,Page page){
        page.setTotalResult(num+pageCount);
        return num+pageCount;
    }
}
